﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxControlToolkit;
using AmkeniCrossLoginApplication.Data_Access;

namespace Amkeni_System.CO
{
    public partial class Call_For_Proposals : System.Web.UI.Page
    {
        Amkeni_DatabaseEntities db = new Amkeni_DatabaseEntities();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

            }
        }

        protected void Accordion1_ItemDataBound(object sender, AjaxControlToolkit.AccordionItemEventArgs e)
        {
            if (e.ItemType == AccordionItemType.Content)
            {
                Label proj_label = (Label)e.AccordionItem.FindControl("proj_label");
                GridView grid1 = (GridView)e.AccordionItem.FindControl("ProjectGridView");
                GridView grid2 = (GridView)e.AccordionItem.FindControl("GridView1");
                BindGrid(grid1, proj_label.Text.Trim());
                BindGrid(grid2, proj_label.Text.Trim());
            }
        }

        private void BindGrid(GridView grid, string rfp_id)
        {
            try
            {
                int id = int.Parse(rfp_id.Trim());

                var myIPs = (from p in db.implementing_partner_metadata where p.RFP_id == id select new 
                { 
                    p.implementing_partner.ip_id,
                    p.implementing_partner.name,
                    p.implementing_partner.abbreviation,
                    p.RFP_id
                });
                grid.DataSource = myIPs.ToList();
                grid.DataBind();
            }
            catch(Exception ex)
            {
                Error.Text = "Database Error " + ex.Message + " " + ex.StackTrace;
                Error.Visible = true;
                //throw new NotImplementedException();
            }
        }

        protected void ProjectGridView_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView myGrid = (GridView)sender;
            myGrid.EditIndex = e.NewEditIndex;

            //.Values[1] represents the RFP_id datakeyname
            //Values[ip_id, RFP_id]
            BindGrid(myGrid, myGrid.DataKeys[0].Values[1].ToString());
        }

        protected void ProjectGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridView myGrid = (GridView)sender;
            try
            {
                TextBox ProjectTX = (TextBox)myGrid.Rows[e.RowIndex].FindControl("TxProject");
                TextBox AbbrevTX = (TextBox)myGrid.Rows[e.RowIndex].FindControl("TxAbbreviation");
                int ProjID = int.Parse(myGrid.DataKeys[e.RowIndex].Values[0].ToString());

                implementing_partner updateProject = db.implementing_partner.First(p => p.ip_id == ProjID);
                updateProject.name = ProjectTX.Text.Trim();
                updateProject.abbreviation = AbbrevTX.Text.Trim();
                db.SaveChanges();

                myGrid.EditIndex = -1;
                BindGrid(myGrid, myGrid.DataKeys[e.RowIndex].Values[1].ToString());
            }
            catch (Exception ex)
            {
                Error.Text = "Database Error " + ex.Message + " " + ex.StackTrace;
                Error.Visible = true;
            }
        }

        protected void ProjectGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView myGrid = (GridView)sender;
            myGrid.EditIndex = -1;
            BindGrid(myGrid, myGrid.DataKeys[0].Values[1].ToString());
        }

        protected void ProjectGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridView myGrid = (GridView)sender;
            try
            {
                long ProjID = long.Parse(myGrid.DataKeys[e.RowIndex].Values[0].ToString());

                implementing_partner delProject = db.implementing_partner.First(p => p.ip_id == ProjID);

                db.implementing_partner.Remove(delProject);
                db.SaveChanges();

                BindGrid(myGrid, myGrid.DataKeys[e.RowIndex].Values[1].ToString());
            }
            catch (Exception ex)
            {
                Error.Text = "Database Error " + ex.Message + " " + ex.StackTrace;
                Error.Visible = true;
            }
        }

        protected void AddCallButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Amkeni_System/CO/Edit/NewCall.aspx", true);
        }

        protected void AddIPtoCallButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Amkeni_System/CO/Edit/AddIPtoCall.aspx", true);
        }
    }
}